package com.douwong.student.activity;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.Toast;

import com.douwong.student.base.BaseActivity;
import com.douwong.student.utils.UIUtils;

import java.io.File;

public class CaptureActivity extends BaseActivity {

	
//	ImageView img_display=null;
	int CAMERA_REQUEST_CODE=2001;
	int ALBUM_REQUEST_CODE=81;
	String tmpPicPath=null;


	@Override
	protected void initView() {
		tmpPicPath= Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+ UIUtils.getUUidString()+".jpg";
		int route=getIntent().getIntExtra("router", 0);

		if(route==0){
			showCapture();
		}else{
			findPicture();
		}

	}


	
	
	void showCapture(){
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_OUTPUT,
				Uri.fromFile(new File(tmpPicPath)));
		startActivityForResult(intent, CAMERA_REQUEST_CODE);
	}

	void findPicture(){
		Intent intent2 = new Intent(Intent.ACTION_PICK,
				MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		startActivityForResult(intent2, ALBUM_REQUEST_CODE);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode==RESULT_OK){
			if(requestCode==ALBUM_REQUEST_CODE){
				Uri selectedImage = data.getData();
				String[] filePathColumns = { MediaStore.Images.Media.DATA };
				Cursor c = this.getContentResolver().query(selectedImage, filePathColumns, null, null, null);
				int i = 0;
				if (c.moveToNext()) {
					int columnIndex = c.getColumnIndex(filePathColumns[i]);
					tmpPicPath = c.getString(columnIndex);					
				}else{
					Toast.makeText(this, "没图片######", Toast.LENGTH_LONG).show();
				}
			}else if(requestCode==CAMERA_REQUEST_CODE){
				Intent intent=new Intent();
				intent.putExtra("picPath", tmpPicPath);
				setResult(RESULT_OK,intent);				
			}			
//			Bitmap bitmap=BitmapDecoder.decodeSampledBitmapFromFile(tmpPicPath, new BitmapSize(500, 500), Config.RGB_565);			
//			img_display.setImageBitmap(bitmap);			
		}else{			
//			Toast.makeText(this, "######result fail#######", Toast.LENGTH_LONG).show();
		}
		finish();
	}
}
